/*
 * SuperBlock.cpp
 *
 *  Created on: Mar 23, 2010
 *      Author: Lukasz Spintzyk
 */

#include "SuperBlock.h"
#include "Inode.h"
#include "version.h"


SuperBlock::SuperBlock()
    :d_magicNumber(MAGIC_NUMBER)
     ,d_revisionNumber(REVISION_NUMEBER)
     ,d_freeBlocks(0)
     ,d_freeInodes(0)
{
}

SuperBlock::~SuperBlock()
{
}

bool SuperBlock::operator==(const SuperBlock& other)
{
	bool ret = true;

    ret = (d_revisionNumber == other.d_revisionNumber) && ret;
    ret = (d_blockSize == other.d_blockSize) && ret;
    ret = (d_inodesCount == other.d_inodesCount) && ret;
    ret = (d_blocksCount == other.d_blocksCount) && ret;
    ret = (this->reservedBlocks() == other.reservedBlocks()) && ret;
    ret = (d_freeBlocks == other.d_freeBlocks) && ret;
    ret = (d_freeInodes == other.d_freeInodes) && ret;
    ret = (d_firstDataBlock == other.d_firstDataBlock) && ret;
    ret = (d_logBlockSize == other.d_logBlockSize) && ret;
    ret = (d_blocksPerGroup == other.d_blocksPerGroup) && ret;
    ret = (d_inodesPerGroup == other.d_inodesPerGroup) && ret;
    ret = (d_mtime == other.d_mtime) && ret;
    ret = (d_wtime == other.d_wtime) && ret;
    ret = (d_mntCount == other.d_mntCount) && ret;
    ret = (d_maxMntCount == other.d_maxMntCount) && ret;
    ret = (d_magicNumber == other.d_magicNumber) && ret;
    ret = (d_state == other.d_state) && ret;
    ret = (d_errors == other.d_errors) && ret;
    ret = (d_lastCheck == other.d_lastCheck) && ret;
    ret = (d_checkInterval == other.d_checkInterval) && ret;
    ret = (d_CreatorOs == other.d_CreatorOs) && ret;
    ret = (d_defResUID == other.d_defResUID) && ret;
    ret = (d_defResGID == other.d_defResGID) && ret;

	return ret;
}

uint32_t SuperBlock::size()
{
    return sizeof(SuperBlock);
}

uint32_t SuperBlock::getInodeTableBlockOffset()
{
	return ((d_blockSize == 1024) ? 3 : 2) // 1024 not used bytes + superblock + groupDesc
			+ 1	// BlockBitmap
			+ 1;// inodeBitmap
}

void SuperBlock::read(byte* buffer)
{
    memcpy((void*)this, buffer, sizeof(SuperBlock));
}

void SuperBlock::write(byte* buffer)
{
    memcpy(buffer, this, sizeof(SuperBlock));
}


uint32_t SuperBlock::getInodesPerBlock()
{
	return d_blockSize/Inode::size();
}

uint32_t SuperBlock::reservedBlocks() const
{
	return d_blocksCount -d_freeBlocks;
}
